/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @author: jin Chen 
 * @date: Sep 2, 2004$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.plot.layer;

import edu.psu.geovista.app.pcp.plot.layer.operation.BasicLayerOp;
import edu.psu.geovista.app.pcp.plot.layer.operation.DrawLineOp;
import edu.psu.geovista.app.pcp.plot.layer.operation.HighLightLineOp;
import edu.psu.geovista.app.pcp.plot.layer.operation.PreviewAxisOp;
import edu.psu.geovista.app.plot.axis.YAxis;

import java.awt.*;
import java.util.Hashtable;

public class TopLayer extends BasicLayer{
    // indication=new Indication();
    BasicLayerOp operator;
    Hashtable operators;

    public TopLayer(String name) {
        this.setName(name);
        this.setPersisted(false);
        setupOperator();
    }


    private  void setupOperator() {
        Object[] opts={
            "PreviewAxisOp",new PreviewAxisOp(),
            "DrawLineOp",new DrawLineOp(),
            "HighLightLineOp",new HighLightLineOp(this)
        } ;

        this.operators =new Hashtable();
        for(int i = 0; i < opts.length; i += 2) {
            Object value = opts[i + 1];
            if (value == null) {
                this.operators.remove(opts[i]);
            }
            else {
                this.operators.put(opts[i], value);
            }
        }
    }
     /*******************************************************************************************************
     *                Paint functions
     *******************************************************************************************************/
    /**
     *  For ordering axis by dragging. Paint a dummy axis
     * @param axis
     * @param from
     * @param to       x location
     */
    public void  previewMoveAxis(YAxis axis, int from, int to) {
        this.clearLayer() ;
        //this.image =null;
        PreviewAxisOp pa=(PreviewAxisOp)this.operators.get("PreviewAxisOp");
        pa.setAxis(axis);
        pa.setLocation(to,Double.NaN);//y depend on axis's y
        this.setOperator(pa) ;

        //this.setRepaint(true);
    }
    /**
     * Paint brushing range
     * @param from start of brushing
     * @param to   end of brushing
     */
    public void drawBrushing(Point from, Point to) {
        DrawLineOp dl=(DrawLineOp) this.operators.get("DrawLineOp");
        dl.drawLine(from, to);
        this.setOperator(dl);

    }

    private  void setOperator(BasicLayerOp operator) {
        this.operator = operator;
    }

    public void  hightlightLine(String rowName,int id,int x, int y){
        HighLightLineOp hll=(HighLightLineOp)this.operators.get("HighLightLineOp");
        hll.setRecordId(id);
        hll.setName(rowName);
        hll.setX(x);
        hll.setY(y);
        ConfigInfo cfg=this.getConfigInfo() ;
        Color color=(Color) cfg.getConfig(ConfigInfo.Attr_COLOR );
        if(color!=null)
            hll.setColor(color);
        this.setOperator(hll);


    }

    public  void paintComponent(Graphics g) {
        if (this.operator !=null)
            operator.paint(g);
    }

   



   /* public void  hightlightLine(String rowName,int id,int x, int y){
        ConfigInfo cfg=this.getConfigInfo() ;
        Color color=(Color) cfg.getConfig(ConfigInfo.COLOR );
        Indication ind=indication;
        ind.color =color;
        ind.id = id;
        ind.name =rowName;
        ind.x = x;
        ind.y = y;


    }

    class Indication{
        String name;
        int id;
        int x,y;
        Color color;
    }*/
}
